﻿using suirui.ZhuMu.Log;
using suirui.ZhuMu.Model;
using suirui.ZhuMu.Entity;
using suirui.ZhuMu.Entity.Entities;
using suirui.ZhuMu.Common;
using System;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Text;

namespace suirui.ZhuMu.Repositories
{
    public class LoginRepository : BaseRepository<User>, ILoginRepository
    {
        private RSAUtils rsaHelper { get; set; }
        public LoginRepository(mjgamesContext context)
            : base(context)
        {
            this.rsaHelper = new RSAUtils(RSAType.RSA2, Encoding.UTF8);
        }

        public async Task<APIResult<User>> Login(LoginDTO login)
        {
            var user = await GetAsync(t => t.Email == login.Email &&
                                            t.Password == login.Password);
            if (user == null)
            {
                return new APIResult<User>() { Code = ErrorCode.UserNotExist, Message = "无效的登录信息，请确认后再登录！" };
            }
            if (!user.Password.Equals(rsaHelper.Decrypt(login.Password)))
            {
                return new APIResult<User>() { Code = ErrorCode.PasswordError, Message = "密码错误，请确认后再登录！" };
            }
            user.Password = "";
            return new APIResult<User>() { Data = user, Code = ErrorCode.Success, Message = "登录成功" };
        }

    }
}
